package com.tim.app.ui.adapter.story;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.application.library.net.StringResponseCallback;
import com.application.library.util.TimeUtil;
import com.application.library.widget.RatioImageView;
import com.application.library.widget.recycle.BaseRecyclerAdapter;
import com.application.library.widget.roundimg.RoundedImageView;
import com.google.gson.Gson;
import com.tim.app.R;
import com.tim.app.constant.AppKey;
import com.tim.app.server.api.API_User;
import com.tim.app.server.entry.ActionStateMsg;
import com.tim.app.server.entry.Dynamic;
import com.tim.app.server.entry.User;
import com.tim.app.server.logic.UserManager;
import com.tim.app.server.result.ActionResult;
import com.tim.app.server.result.ResponseResult;
import com.tim.app.ui.activity.DynamicDetailActivity;
import com.tim.app.ui.activity.some_homepage.SomeoneHomepageActivity;
import com.tim.app.ui.fragment.home.HomeDynamicFragment;
import com.tim.app.util.BitmapLoader;
import com.tim.app.util.ToastUtil;
import com.tim.app.util.ViewGT;

import java.util.List;


/**
 * 动态
 */
public class DynamicAdapter extends BaseRecyclerAdapter<BaseRecyclerAdapter.BaseRecyclerViewHolder, Dynamic> {

    private Context mContext;


    public DynamicAdapter(Context mContext, List<Dynamic> mDataList) {
        super(mDataList);
        this.mContext = mContext;
    }

    @Override
    public BaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseRecyclerViewHolder holder = null;
        holder = new DynamicViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_dynamic_item, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final BaseRecyclerViewHolder holder, int position, Dynamic data) {
        if (data == null) {
            return;
        }
        DynamicViewHolder dynamicViewHolder = (DynamicViewHolder) holder;
        updateDynamicHolder(dynamicViewHolder, position, data);
    }

    /**
     * 更新动态UI
     *
     * @param holder
     * @param position
     * @param data
     */
    private void updateDynamicHolder(final DynamicViewHolder holder, final int position, final Dynamic data) {
        if (!TextUtils.isEmpty(data.getContent())) {
            holder.tvDynamicTitle.setVisibility(View.VISIBLE);
            holder.tvDynamicTitle.setText(data.getContent());
        } else {
            holder.tvDynamicTitle.setText("");
            holder.tvDynamicTitle.setVisibility(View.GONE);
        }
        if (null != data.getUserMsg()) {
            if (!TextUtils.isEmpty(data.getUserMsg().getName())) {
                holder.tvUserName.setText(data.getUserMsg().getName());
            } else {
                holder.tvUserName.setText("");
            }
            BitmapLoader.ins().loadImage(data.getUserMsg().getImage(), holder.rivAvatar);
            holder.rivAvatar.setTag(data.getUserMsg());
            holder.tvUserName.setTag(data.getUserMsg());

            holder.rivAvatar.setOnClickListener(onClickListener);
            holder.tvUserName.setOnClickListener(onClickListener);
        } else {
            holder.tvUserName.setText("");
            BitmapLoader.ins().loadImage("", holder.rivAvatar);
        }

        if (null != data.getActionStateMsg()) {
            if (null != data.getActionStateMsg().getAgreeState()) {
                holder.ivPrise.setSelected(true);
            }else {
                holder.ivPrise.setSelected(false);
            }
        } else {
            holder.ivPrise.setSelected(false);
        }
        holder.tvPriseNumber.setText(mContext.getString(R.string.selectPriseNumber, String.valueOf(data.getAgreeCount())));
        holder.tvCommentNumber.setText(mContext.getString(R.string.selectCommentNumber, String.valueOf(data.getCommentCount())));
        holder.tvPublishTime.setText(TimeUtil.formatTimeRuleTwo(mContext, data.getCreateTime()) + mContext.getString(R.string.pointPrompt));

        holder.ivPrise.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!UserManager.ins().isLogin()) {
                    ViewGT.gotoLoginActivity(mContext);
                    return;
                }
                addPriseOrCancelPrise(holder.ivPrise, data, position);
            }
        });

        holder.ivComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DynamicDetailActivity.startWithShowKeyboard(mContext, data, true);
            }
        });

        if (null != data.getImageUrl()) {
            if (data.getImageUrl().size() == 0) {
                holder.rlImageContainer.setVisibility(View.GONE);
            } else {
                holder.rlImageContainer.setVisibility(View.VISIBLE);
                if (data.getImageUrl().size() == 1) {
                    holder.rivDynamicOne.setVisibility(View.VISIBLE);
                    holder.icContainerTwo.setVisibility(View.GONE);
                    holder.icContainerThree.setVisibility(View.GONE);
                    holder.icContainerFour.setVisibility(View.GONE);
                    holder.icContainerFive.setVisibility(View.GONE);
                    holder.icContainerSix.setVisibility(View.GONE);
                    BitmapLoader.ins().loadImage(data.getImageUrl().get(0), holder.rivDynamicOne);
                } else if (data.getImageUrl().size() == 2) {
                    holder.rivDynamicOne.setVisibility(View.GONE);
                    holder.icContainerTwo.setVisibility(View.VISIBLE);
                    holder.icContainerThree.setVisibility(View.GONE);
                    holder.icContainerFour.setVisibility(View.GONE);
                    holder.icContainerFive.setVisibility(View.GONE);
                    holder.icContainerSix.setVisibility(View.GONE);
                    BitmapLoader.ins().loadImage(data.getImageUrl().get(0), holder.rivDynamicTwo_one);
                    BitmapLoader.ins().loadImage(data.getImageUrl().get(1), holder.rivDynamicTwo_two);
                } else if (data.getImageUrl().size() == 3) {
                    holder.rivDynamicOne.setVisibility(View.GONE);
                    holder.icContainerTwo.setVisibility(View.GONE);
                    holder.icContainerThree.setVisibility(View.VISIBLE);
                    holder.icContainerFour.setVisibility(View.GONE);
                    holder.icContainerFive.setVisibility(View.GONE);
                    holder.icContainerSix.setVisibility(View.GONE);
                    BitmapLoader.ins().loadImage(data.getImageUrl().get(0), holder.rivDynamicThree_one);
                    BitmapLoader.ins().loadImage(data.getImageUrl().get(1), holder.rivDynamicThree_two);
                    BitmapLoader.ins().loadImage(data.getImageUrl().get(2), holder.rivDynamicThree_three);
                } else if (data.getImageUrl().size() == 4) {
                    holder.rivDynamicOne.setVisibility(View.GONE);
                    holder.icContainerTwo.setVisibility(View.GONE);
                    holder.icContainerThree.setVisibility(View.GONE);
                    holder.icContainerFour.setVisibility(View.VISIBLE);
                    holder.icContainerFive.setVisibility(View.GONE);
                    holder.icContainerSix.setVisibility(View.GONE);
                    BitmapLoader.ins().loadImage(data.getImageUrl().get(0), holder.rivDynamicFour_one);
                    BitmapLoader.ins().loadImage(data.getImageUrl().get(1), holder.rivDynamicFour_two);
                    BitmapLoader.ins().loadImage(data.getImageUrl().get(2), holder.rivDynamicFour_three);
                    BitmapLoader.ins().loadImage(data.getImageUrl().get(3), holder.rivDynamicFour_four);
                } else if (data.getImageUrl().size() == 5) {
                    holder.rivDynamicOne.setVisibility(View.GONE);
                    holder.icContainerTwo.setVisibility(View.GONE);
                    holder.icContainerThree.setVisibility(View.GONE);
                    holder.icContainerFour.setVisibility(View.VISIBLE);
                    holder.icContainerFive.setVisibility(View.VISIBLE);
                    holder.icContainerSix.setVisibility(View.GONE);
                    BitmapLoader.ins().loadImage(data.getImageUrl().get(0), holder.rivDynamicFive_one);
                    BitmapLoader.ins().loadImage(data.getImageUrl().get(1), holder.rivDynamicFive_two);
                    BitmapLoader.ins().loadImage(data.getImageUrl().get(2), holder.rivDynamicFive_three);
                    BitmapLoader.ins().loadImage(data.getImageUrl().get(3), holder.rivDynamicFive_four);
                    BitmapLoader.ins().loadImage(data.getImageUrl().get(4), holder.rivDynamicFive_five);
                } else if (data.getImageUrl().size() == 6) {
                    holder.rivDynamicOne.setVisibility(View.GONE);
                    holder.icContainerTwo.setVisibility(View.GONE);
                    holder.icContainerThree.setVisibility(View.GONE);
                    holder.icContainerFour.setVisibility(View.GONE);
                    holder.icContainerFive.setVisibility(View.GONE);
                    holder.icContainerSix.setVisibility(View.VISIBLE);
                    BitmapLoader.ins().loadImage(data.getImageUrl().get(0), holder.rivDynamicSix_one);
                    BitmapLoader.ins().loadImage(data.getImageUrl().get(1), holder.rivDynamicSix_two);
                    BitmapLoader.ins().loadImage(data.getImageUrl().get(2), holder.rivDynamicSix_three);
                    BitmapLoader.ins().loadImage(data.getImageUrl().get(3), holder.rivDynamicSix_four);
                    BitmapLoader.ins().loadImage(data.getImageUrl().get(4), holder.rivDynamicSix_five);
                    BitmapLoader.ins().loadImage(data.getImageUrl().get(5), holder.rivDynamicSix_six);
                }
            }
        } else {
            holder.rlImageContainer.setVisibility(View.GONE);
        }
    }

    /**
     * 赞/取消赞
     */
    public void addPriseOrCancelPrise(final View view, final Dynamic dynamic, final int position) {
        view.setEnabled(false);
        if (null != dynamic.getActionStateMsg() && dynamic.getActionStateMsg().getAgreeState() != null) {
            int id = dynamic.getActionStateMsg().getAgreeState();
            API_User.ins().cancelPrise(HomeDynamicFragment.TAG, UserManager.ins().getUserId(), AppKey.TYPE_DYNAMIC, dynamic.getId(), AppKey.AGREE, id, dynamic.getUserMsg().getUserId(), new StringResponseCallback() {
                @Override
                public boolean onStringResponse(String resultValue, int errCode, String errMsg, int id, boolean formCache) {
                    view.setEnabled(true);
                    if (errCode == ResponseResult.ResultStatus.OK && resultValue != null) {
                        dynamic.setAgreeCount(dynamic.getAgreeCount() - 1);
                        dynamic.getActionStateMsg().setAgreeState(null);
                        getDataList().set(position, dynamic);
                        notifyItemChanged(position);
                    } else {
                        ToastUtil.showToast(errMsg);
                    }
                    return false;
                }
            });
        } else {
            API_User.ins().addPrise(HomeDynamicFragment.TAG, UserManager.ins().getUserId(), AppKey.TYPE_DYNAMIC, dynamic.getId(), AppKey.AGREE, dynamic.getUserMsg().getUserId(), new StringResponseCallback() {
                @Override
                public boolean onStringResponse(String resultValue, int errCode, String errMsg, int id, boolean formCache) {
                    view.setEnabled(true);
                    if (errCode == ResponseResult.ResultStatus.OK && resultValue != null) {
                        ActionResult result = new Gson().fromJson(resultValue, ActionResult.class);
                        if (null != result && null != result.getRes()) {
                            if (null == dynamic.getActionStateMsg()) {
                                dynamic.setActionStateMsg(new ActionStateMsg());
                            }
                            dynamic.getActionStateMsg().setAgreeState(result.getRes().getAction().getId());
                            dynamic.setAgreeCount(dynamic.getAgreeCount() + 1);
                            getDataList().set(position, dynamic);
                            notifyItemChanged(position);
                        }
                    } else {
                        ToastUtil.showToast(errMsg);
                    }
                    return false;
                }
            });
        }
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            User user = (User) v.getTag();
            if (null != user)
                SomeoneHomepageActivity.start(mContext, user, false);
        }
    };

    /**
     * 动态
     */
    class DynamicViewHolder extends BaseRecyclerViewHolder {

        RoundedImageView rivAvatar;
        TextView tvUserName;
        TextView tvDynamicTitle, tvPriseNumber, tvCommentNumber, tvPublishTime;

        RelativeLayout rlImageContainer;
        RatioImageView rivDynamicOne;

        View icContainerTwo;
        RatioImageView rivDynamicTwo_one;
        RatioImageView rivDynamicTwo_two;

        View icContainerThree;
        RatioImageView rivDynamicThree_one;
        RatioImageView rivDynamicThree_two;
        RatioImageView rivDynamicThree_three;

        View icContainerFour;
        RatioImageView rivDynamicFour_one;
        RatioImageView rivDynamicFour_two;
        RatioImageView rivDynamicFour_three;
        RatioImageView rivDynamicFour_four;

        View icContainerFive;
        RatioImageView rivDynamicFive_one;
        RatioImageView rivDynamicFive_two;
        RatioImageView rivDynamicFive_three;
        RatioImageView rivDynamicFive_four;
        RatioImageView rivDynamicFive_five;

        View icContainerSix;
        RatioImageView rivDynamicSix_one;
        RatioImageView rivDynamicSix_two;
        RatioImageView rivDynamicSix_three;
        RatioImageView rivDynamicSix_four;
        RatioImageView rivDynamicSix_five;
        RatioImageView rivDynamicSix_six;

        ImageView ivPrise;
        ImageView ivComment;

        public DynamicViewHolder(View itemView) {
            super(itemView);
            rivAvatar = (RoundedImageView) itemView.findViewById(R.id.rivAvatar);
            tvUserName = (TextView) itemView.findViewById(R.id.tvUserName);
            tvDynamicTitle = (TextView) itemView.findViewById(R.id.tvDynamicTitle);
            tvPriseNumber = (TextView) itemView.findViewById(R.id.tvPriseNumber);
            tvCommentNumber = (TextView) itemView.findViewById(R.id.tvCommentNumber);
            tvPublishTime = (TextView) itemView.findViewById(R.id.tvPublishTime);

            rlImageContainer = (RelativeLayout) itemView.findViewById(R.id.rlImageContainer);
            rivDynamicOne = (RatioImageView) itemView.findViewById(R.id.icContainerOne);

            icContainerTwo = itemView.findViewById(R.id.icContainerTwo);
            rivDynamicTwo_one = (RatioImageView) itemView.findViewById(R.id.rivDynamicTwo_one);
            rivDynamicTwo_two = (RatioImageView) itemView.findViewById(R.id.rivDynamicTwo_two);

            icContainerThree = itemView.findViewById(R.id.icContainerThree);
            rivDynamicThree_one = (RatioImageView) itemView.findViewById(R.id.rivDynamicThree_one);
            rivDynamicThree_two = (RatioImageView) itemView.findViewById(R.id.rivDynamicThree_two);
            rivDynamicThree_three = (RatioImageView) itemView.findViewById(R.id.rivDynamicThree_three);

            icContainerFour = itemView.findViewById(R.id.icContainerFour);
            rivDynamicFour_one = (RatioImageView) itemView.findViewById(R.id.rivDynamicFour_one);
            rivDynamicFour_two = (RatioImageView) itemView.findViewById(R.id.rivDynamicFour_two);
            rivDynamicFour_three = (RatioImageView) itemView.findViewById(R.id.rivDynamicFour_three);
            rivDynamicFour_four = (RatioImageView) itemView.findViewById(R.id.rivDynamicFour_four);

            icContainerFive = itemView.findViewById(R.id.icContainerFive);
            rivDynamicFive_one = (RatioImageView) itemView.findViewById(R.id.rivDynamicFive_one);
            rivDynamicFive_two = (RatioImageView) itemView.findViewById(R.id.rivDynamicFive_two);
            rivDynamicFive_three = (RatioImageView) itemView.findViewById(R.id.rivDynamicFive_three);
            rivDynamicFive_four = (RatioImageView) itemView.findViewById(R.id.rivDynamicFive_four);
            rivDynamicFive_five = (RatioImageView) itemView.findViewById(R.id.rivDynamicFive_five);

            icContainerSix = itemView.findViewById(R.id.icContainerSix);
            rivDynamicSix_one = (RatioImageView) itemView.findViewById(R.id.rivDynamicSix_one);
            rivDynamicSix_two = (RatioImageView) itemView.findViewById(R.id.rivDynamicSix_two);
            rivDynamicSix_three = (RatioImageView) itemView.findViewById(R.id.rivDynamicSix_three);
            rivDynamicSix_four = (RatioImageView) itemView.findViewById(R.id.rivDynamicSix_four);
            rivDynamicSix_five = (RatioImageView) itemView.findViewById(R.id.rivDynamicSix_five);
            rivDynamicSix_six = (RatioImageView) itemView.findViewById(R.id.rivDynamicSix_six);

            ivPrise = (ImageView) itemView.findViewById(R.id.ivPrise);
            ivComment = (ImageView) itemView.findViewById(R.id.ivComment);
        }

    }

}
